﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Library.DAL
{
    public class SanPhamDAL
    {
        public static int CountAll()
        {
            DAL.NETMobileEntities db = new NETMobileEntities();
            return db.SanPhams.Count();
        }
        public static List<BIZ.SanPhamBIZ> FindAll(long gia1, long gia2)
        {
            DAL.NETMobileEntities db = new NETMobileEntities();
            BIZ.SanPhamBIZ sp;
            List<BIZ.SanPhamBIZ> listsp = new List<BIZ.SanPhamBIZ>();

            if (gia1 == -1 && gia2 == -1)
            {
                return FindAll(null, null);
            }
            else if (gia2 != -1 && gia1 == -1)
            {
                var r = from a in db.SanPhams
                        where a.Gia <= gia2
                        select a;

                foreach (DAL.SanPham s in r)
                {
                    sp = new BIZ.SanPhamBIZ();
                    sp.ID = s.ID;
                    sp.MaSP = s.MaSP;
                    sp.TenSP = s.TenSP;
                    sp.MoTa = s.MoTa;
                    sp.HangSanXuat = s.HangSanXuat;
                    sp.Gia = s.Gia;
                    sp.HinhAnh = s.HinhAnh;
                    listsp.Add(sp);
                }
            }
            else if (gia1 != -1 && gia2 == -1)
            {
                var r = from a in db.SanPhams
                        where a.Gia >= gia1
                        select a;

                foreach (DAL.SanPham s in r)
                {
                    sp = new BIZ.SanPhamBIZ();
                    sp.ID = s.ID;
                    sp.MaSP = s.MaSP;
                    sp.TenSP = s.TenSP;
                    sp.MoTa = s.MoTa;
                    sp.HangSanXuat = s.HangSanXuat;
                    sp.Gia = s.Gia;
                    sp.HinhAnh = s.HinhAnh;
                    listsp.Add(sp);
                }
            }
            else
            {
                var r = from a in db.SanPhams
                        where a.Gia >= gia1 && a.Gia <= gia2
                        select a;

                foreach (DAL.SanPham s in r)
                {
                    sp = new BIZ.SanPhamBIZ();
                    sp.ID = s.ID;
                    sp.MaSP = s.MaSP;
                    sp.TenSP = s.TenSP;
                    sp.MoTa = s.MoTa;
                    sp.HangSanXuat = s.HangSanXuat;
                    sp.Gia = s.Gia;
                    sp.HinhAnh = s.HinhAnh;
                    listsp.Add(sp);
                }
            }
            return listsp;
        }
        public static List<BIZ.SanPhamBIZ> FindAll(string masp, string tensp)
        {
            DAL.NETMobileEntities db = new NETMobileEntities();
            BIZ.SanPhamBIZ sp;
            List<BIZ.SanPhamBIZ> listsp = new List<BIZ.SanPhamBIZ>();

            if (String.IsNullOrEmpty(masp) && String.IsNullOrEmpty(tensp))
            {
                var r = from a in db.SanPhams select a;
                foreach (DAL.SanPham s in r)
                {
                    sp = new BIZ.SanPhamBIZ();
                    sp.ID = s.ID;
                    sp.MaSP = s.MaSP;
                    sp.TenSP = s.TenSP;
                    sp.MoTa = s.MoTa;
                    sp.HangSanXuat = s.HangSanXuat;
                    sp.Gia = s.Gia;
                    sp.HinhAnh = s.HinhAnh;
                    listsp.Add(sp);
                }
            }
            else if (String.IsNullOrEmpty(masp))
            {
                // nếu tên sản phẩm khác null
                var r = from a in db.SanPhams
                        where a.TenSP.Contains(tensp)
                        select a;
                foreach (DAL.SanPham s in r)
                {
                    sp = new BIZ.SanPhamBIZ();
                    sp.ID = s.ID;
                    sp.MaSP = s.MaSP;
                    sp.TenSP = s.TenSP;
                    sp.MoTa = s.MoTa;
                    sp.HangSanXuat = s.HangSanXuat;
                    sp.Gia = s.Gia;
                    sp.HinhAnh = s.HinhAnh;
                    listsp.Add(sp);
                }
            }
            else if (String.IsNullOrEmpty(tensp))
            {
                // nếu mã sản phẩm khác null
                var r = from a in db.SanPhams
                        where a.MaSP == masp
                        select a;
                foreach (DAL.SanPham s in r)
                {
                    sp = new BIZ.SanPhamBIZ();
                    sp.ID = s.ID;
                    sp.MaSP = s.MaSP;
                    sp.TenSP = s.TenSP;
                    sp.MoTa = s.MoTa;
                    sp.HangSanXuat = s.HangSanXuat;
                    sp.Gia = s.Gia;
                    sp.HinhAnh = s.HinhAnh;
                    listsp.Add(sp);
                }
            }
            else
            {
                // nếu cả mã sản phẩm và tên sản phẩm đều khác null
                var r = from a in db.SanPhams
                        where a.MaSP == masp && a.TenSP.Contains(tensp)
                        select a;
                foreach (DAL.SanPham s in r)
                {
                    sp = new BIZ.SanPhamBIZ();
                    sp.ID = s.ID;
                    sp.MaSP = s.MaSP;
                    sp.TenSP = s.TenSP;
                    sp.MoTa = s.MoTa;
                    sp.HangSanXuat = s.HangSanXuat;
                    sp.Gia = s.Gia;
                    sp.HinhAnh = s.HinhAnh;
                    listsp.Add(sp);
                }
            }

            return listsp;
        }

        public static bool Delete(BIZ.SanPhamBIZ sp)
        {
            NETMobileEntities db = new NETMobileEntities();
            var r = (from a in db.SanPhams
                    where a.MaSP == sp.MaSP
                    select a).First();
            try
            {
                db.SanPhams.DeleteObject(r);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        public static bool Update(BIZ.SanPhamBIZ sp)
        {
            try
            {
                NETMobileEntities db = new NETMobileEntities();
                var r = (from a in db.SanPhams
                         where a.ID == sp.ID
                         select a).First();
                r.MaSP = sp.MaSP;
                r.HangSanXuat = sp.HangSanXuat;
                r.HinhAnh = sp.HinhAnh;
                r.MoTa = sp.MoTa;
                r.Gia = sp.Gia;
                r.TenSP = sp.TenSP;
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static BIZ.SanPhamBIZ GetByID(int id)
        {
            try
            {
                NETMobileEntities db = new NETMobileEntities();
                var r = (from a in db.SanPhams where a.ID == id select a).First();
                BIZ.SanPhamBIZ s = new BIZ.SanPhamBIZ();

                s.ID = r.ID;
                s.MaSP = r.MaSP;
                s.TenSP = r.TenSP;
                s.HangSanXuat = r.HangSanXuat;
                s.MoTa = r.MoTa;
                s.Gia = r.Gia;
                s.HinhAnh = r.HinhAnh;

                return s;
            }
            catch
            {
                return null;
            }
        }

        public static BIZ.SanPhamBIZ GetByMaSP(string masp)
        {
            try
            {
                NETMobileEntities db = new NETMobileEntities();
                var r = (from a in db.SanPhams where a.MaSP == masp select a).First();
                BIZ.SanPhamBIZ s = new BIZ.SanPhamBIZ();

                s.ID = r.ID;
                s.MaSP = r.MaSP;
                s.TenSP = r.TenSP;
                s.HangSanXuat = r.HangSanXuat;
                s.MoTa = r.MoTa;
                s.Gia = r.Gia;
                s.HinhAnh = r.HinhAnh;

                return s;
            }
            catch
            {
                return null;
            }

        }

        public static int Add(BIZ.SanPhamBIZ sp)
        {
            try
            {
                NETMobileEntities db = new NETMobileEntities();
                DAL.SanPham s = new SanPham();
                s.MaSP = sp.MaSP;
                s.TenSP = sp.TenSP;
                s.HangSanXuat = sp.HangSanXuat;
                s.MoTa = sp.MoTa;
                s.Gia = sp.Gia;
                s.HinhAnh = sp.HinhAnh;

                db.SanPhams.AddObject(s);
                db.SaveChanges();

                return s.ID;
            }
            catch
            {
                return -1;
            }
        }
    }
}
